package homework;

// 1、无头单向非循环链表实现
public class SingleLinkedList {
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    private ListNode head;

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(this.head == null){
            this.head = node;
            return;
        }
        ListNode cur = this.head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        if(index < 0 || index > this.size()){
            throw new IndexIlegalException(index + " 下标非法");
        }
        if(index == 0){
            addFirst(data);
            return true;
        }
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
        return true;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(this.head == null){
            System.out.println("无元素可以删除");
            return;
        }
        if(this.head.val == key){
            this.head = this.head.next;
            return;
        }
        ListNode cur = this.head;
        while(cur.next != null){
            if(cur.next.val == key){
                ListNode del =cur.next;
                cur.next = del.next;
                return;
            }
            cur = cur.next;
        }
        System.out.println(key + "不存在");
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(this.head == null){
            System.out.println("无元素可以删除");
            return;
        }

        ListNode cur = this.head.next;
        ListNode preCur = this.head;
        while(cur != null){
            if(cur.val == key){
                preCur.next = cur.next;
                cur = cur.next;
            }else{
                preCur = cur;
                cur = cur.next;
            }
        }
        if(this.head.val == key){
            this.head = this.head.next;
        }
    }

    public void removeAllKey1(int key){
        if(this.head == null){
            System.out.println("没有元素可以删除");
            return;
        }
        while(this.head.val == key){
            this.head = this.head.next;
        }
        ListNode cur = this.head.next;
        ListNode preCur = this.head;
        while(cur != null){
            if(cur.val == key){
                preCur.next = cur.next;
                cur = cur.next;
            }else{
                preCur = cur;
                cur = cur.next;
            }
        }
    }
    //得到单链表的长度
    public int size(){
        ListNode cur = this.head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void display(){
        ListNode cur = this.head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void display(ListNode node){
        while(node != null){
            System.out.print(node.val + " ");
            node = node.next;
        }
        System.out.println();
    }
    public void clear(){
        ListNode cur = this.head;
        ListNode curNext = null;
        while(cur.next != null){
            curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        this.head = null;
    }
    public void clear1(){
        //this.head = null;//这种做法 比较粗暴！
        ListNode cur = this.head;
        ListNode curNext = null;
        while (cur != null) {
            curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        head = null;
    }

    public ListNode findKthTotail(int k){
        if(this.head == null || k <= 0){
            return null;
        }
        ListNode fast = this.head;
        ListNode slow = this.head;
        for(int i = 0; i < k - 1; i++){
            if(fast == null){
                return null;
            }else{
                fast = fast.next;
            }
        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
